package me.feng.servlet

import java.util.logging.Logger
import akka.actor.ActorSystem
import javax.servlet.ServletContextEvent
import javax.servlet.ServletContextListener
import javax.servlet.annotation.WebListener
import me.feng.actors.MainActor
import akka.actor.Props
import me.feng.OschartContext

@WebListener
class AkkaListener extends ServletContextListener {

  private val logger = Logger.getLogger(classOf[AkkaListener].getName());

  override def contextInitialized(event: ServletContextEvent) {
    logger.info("FirstListener created");
    //OschartContext.initContext()
    val actorSystem: ActorSystem = ActorSystem()
    OschartContext.akka = actorSystem
    val main = actorSystem.actorOf(Props[MainActor], "main")
    event.getServletContext().setAttribute("akka", actorSystem)
    event.getServletContext().setAttribute("mainActor", main)
  }

  override def contextDestroyed(event: ServletContextEvent) {
    logger.info("OschartContext destroyed");
    //OschartContext.destroy()
    event.getServletContext().getAttribute("akka").asInstanceOf[ActorSystem].shutdown
  }

}